package images

import (
	"encoding/base64"
	"image"
	"image/png"
	"io/ioutil"
	"os"
)

// SaveImage *image.RGBA to filePath with PNG format.
func SaveImage(img *image.RGBA, filePath string) {
	file, err := os.Create(filePath)
	if err != nil {
		panic(err)
	}
	defer file.Close()
	png.Encode(file, img)
}

func ReadImage2Base64(imgPath string) (string, error) {
	f, e := os.Open(imgPath)
	if e != nil {
		println(e.Error())
	}
	defer f.Close()
	d, e := ioutil.ReadAll(f)
	if e != nil {
		println(e.Error())
	}
	const base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	var coder = base64.NewEncoding(base64Table)
	imgString := coder.EncodeToString(d)
	return imgString, nil
}
